<?php

namespace app\model\user;

use app\base\BaseModel;
use app\model\goods\GoodsModel;
use think\model\concern\SoftDelete;

/**
 * 模型-GoodsCollectUp
 * Class GoodsCollectUp
 * @package app\model
 */
class UserCollectUpModel extends BaseModel
{
    use SoftDelete;
    protected $name = 'user_collect_up';

    /**
     * 获取单条数据
     * @param $user_id
     * @param $goods_id
     * @param $type
     * @return array|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChildInfo($user_id, $goods_id, $type)
    {
        $data = $this
            ->where("user_id", $user_id)
            ->where("goods_id", $goods_id)
            ->where("type", $type)
            ->field("id")
            ->find();
        return $data;
    }

    /**
     *  点赞收藏
     * @param $goods_id
     * @param $type
     * @param $user_id
     * @param $operation
     * @return array
     */
    public function apiUpOrCollect($goods_id, $type, $user_id, $operation = "add")
    {

        // 启动事务
        $this->startTrans();
        try {
            $GoodsModel = new GoodsModel();
            $goods = $GoodsModel
                ->where("goods_status", 1)
                ->where("id", $goods_id)
                ->field("id,goods_up_count,goods_first_id,goods_collect_count")
                ->find();

            if (!isset($goods['id'])) {
                throw new \think\Exception("商品不存在", 0);
            }
            $map = [
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'type' => $type,
            ];
            $info = self::withTrashed()->where($map)->find();
            if (isset($info['id'])) {
                if ($operation == "delete") {
                    //存在就取消
                    $info->delete();
                    if ($type == 1) {
                        //收藏
                        $up_goods['goods_collect_count'] = ($goods['goods_collect_count'] - 1) < 0 ? 0 : ($goods['goods_collect_count'] - 1);
                    } else {
                        //点赞
                        $up_goods['goods_up_count'] = ($goods['goods_up_count'] - 1) < 0 ? 0 : ($goods['goods_up_count'] - 1);
                    }
                    $is_type = false;
                } else {
                    $info->restore();
                    if ($type == 1) {
                        //收藏
                        $up_goods['goods_collect_count'] = $goods['goods_collect_count'] + 1;
                    } else {
                        //点赞
                        $up_goods['goods_up_count'] = $goods['goods_up_count'] + 1;
                    }
                    $is_type = true;
                }
            } else {
                //不存在
                $add['user_id'] = $user_id;
                $add['goods_id'] = $goods_id;
                $add['goods_first_type'] = $goods['goods_first_id'];
                $add['type'] = $type;
                $this->save($add);
                if ($type == 1) {
                    //收藏
                    $up_goods['goods_collect_count'] = $goods['goods_collect_count'] + 1;
                } else {
                    //点赞
                    $up_goods['goods_up_count'] = $goods['goods_up_count'] + 1;
                }
                $is_type = true;
            }
            $up_goods['id'] = $goods['id'];
            $GoodsModel->update($up_goods);
            $this->commit();
            if (isset($up_goods['goods_up_count'])) {
                $count = $up_goods['goods_up_count'];
            } else {
                $count = $up_goods['goods_collect_count'];
            }
            $data['count'] = $count;
            $data['is_type'] = $is_type;
            return ['code' => 1, 'data' => $data, 'msg' => "操作成功"];
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return ['code' => 0, 'msg' => "操作失败"];
        }

    }
}
